package com.mamba.microservice.mybatisdemo.db.datasource;

import com.mamba.microservice.mybatis.autoconfig.MybatisDruidAutoConfiguration;
import com.mamba.microservice.mybatis.utils.DbUtils;
import com.mamba.microservice.mybatisdemo.db.properties.CrawlerDataSourceProperties;
import com.mamba.microservice.mybatisdemo.db.properties.CrawlerMybatisProperties;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.annotation.MapperScans;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

import static com.mamba.microservice.mybatis.autoconfig.MybatisDruidAutoConfiguration.MMS_SQL_SESSION_FACTORY_BEAN_NAME;
import static com.mamba.microservice.mybatisdemo.db.datasource.CrawlerDataSourceBean.CRAWLER_SQL_SESSION_FACTORY_BEAN;

/**
 * Copyright ©2020 360jinrong All Rights Reserved
 * 请添加类/接口的说明：
 *
 * @Package: com.mamba.microservice.mybatisdemo.db
 * @author: fangjin
 * @date: 2020年05月12日 18时09分
 */
@Configuration
@EnableConfigurationProperties({CrawlerDataSourceProperties.class, CrawlerMybatisProperties.class})
@AutoConfigureAfter(MybatisDruidAutoConfiguration.class)
@MapperScans({
        @MapperScan(basePackages = "com.mamba.microservice.mybatisdemo.mapper.mms", sqlSessionFactoryRef = MMS_SQL_SESSION_FACTORY_BEAN_NAME),
        @MapperScan(basePackages = "com.mamba.microservice.mybatisdemo.mapper.crawler", sqlSessionFactoryRef = CRAWLER_SQL_SESSION_FACTORY_BEAN)
})
public class CrawlerDataSourceBean {
    private static final String CRAWLER_DATASOURCE = "crawlerDataSource";
    static final String CRAWLER_SQL_SESSION_FACTORY_BEAN = "crawlerSqlSessionFactoryBean";

    private CrawlerDataSourceProperties crawlerDataSourceProperties;
    private CrawlerMybatisProperties crawlerMybatisProperties;

    public CrawlerDataSourceBean(CrawlerDataSourceProperties crawlerDataSourceProperties, CrawlerMybatisProperties crawlerMybatisProperties) {
        this.crawlerDataSourceProperties = crawlerDataSourceProperties;
        this.crawlerMybatisProperties = crawlerMybatisProperties;
    }


    @Bean(name = CRAWLER_DATASOURCE)
    public DataSource dataSource() {
        return DbUtils.buildDruidDataSource(crawlerDataSourceProperties);
    }


    @Bean(name = CRAWLER_SQL_SESSION_FACTORY_BEAN)
    public SqlSessionFactoryBean sqlSessionFactory(@Qualifier(CRAWLER_DATASOURCE) DataSource dataSource) {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        sqlSessionFactoryBean.setConfigLocation(DbUtils.resolveMybatisConfigResource(crawlerMybatisProperties.getConfigLocation()));
        sqlSessionFactoryBean.setMapperLocations(DbUtils.resolveMapperLocations(crawlerMybatisProperties.getMapperLocations()));
        return sqlSessionFactoryBean;
    }
}
